import {IConsoleLogger} from "./IConsoleLogger";
import { ConnectionParam, LoginParam } from "./JSJaCConnection";
import {JSJaCConsoleLogger} from "./JSJaCConsoleLogger";
import {JSJaCWebSocketConnection} from "./JSJaCWebSocketConnection";


/**
 * 由于websocket是在外面建的,所以这里生成一个websocket代理 
 */
class SDKWebSocket implements WebSocket {
    private ws:WebSocket;
    private oDbg:IConsoleLogger;
    protocol: string;
    readyState: number;
    url: string;
    binaryType: BinaryType;
    bufferedAmount: number;
    extensions: string;
    CLOSED: number;
    CLOSING: number;
    CONNECTING: number;
    OPEN: number;
    constructor(ws:WebSocket,oDbg:IConsoleLogger){
        this.ws=ws
        this.protocol=ws.protocol
        this.url=ws.url
        this.oDbg=oDbg?oDbg:new JSJaCConsoleLogger()
        this.readyState=ws.readyState
        this.extensions=ws.extensions
        
        //判断是<开头的,走openfire的回调

        if(ws.onmessage){
            let cb:Function=ws.onmessage;
            ws.onmessage=(ev:MessageEvent) => {
                //nodeJs 环境,ev是string类型
                /*
                let evTemp=null
                if(JSJaCUtils.NODEJS_RUNTIME){
                    evTemp={data:ev.toString()}
                } else {
                    evTemp=ev
                }
                */
                this.oDbg.debug('ws recieve msg'+ev.data)
                if(ev.data && ev.data.startsWith("<")){
                    this.onmessage(ev)
                } else {
                    cb(ev)
                }
            }
        } else {
            this.oDbg.error('ws.onmessage not set callback function. please check wsclient')
            ws.onmessage=(ev:MessageEvent) => {
                /*
                let evTemp=null
                if(JSJaCUtils.NODEJS_RUNTIME){
                    evTemp={data:ev.toString()}
                } else {
                    evTemp=ev
                }
                */
                if(ev.data && ev.data.startsWith("<")){
                    this.onmessage(ev)
                }  else {
                    this.oDbg.debug('ws.onmessage no handle message:'+ev.data)
                }

            }

        }
    }
    onclose:(ev:CloseEvent)=>void;
    onerror:(ev: Event)=>void;
    onmessage:(ev: MessageEvent|{data:string})=>void;
    onopen:(ev: Event)=>void ;
    close(code?: number, reason?: string):void {
        this.oDbg.debug('close do nothing');
    }
    send(data: string | ArrayBuffer | Blob | ArrayBufferView): void {
        this.ws.send(data)
    }
    addEventListener<K extends keyof WebSocketEventMap>(type: K, listener: (this: WebSocket, ev: WebSocketEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void;
    addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void;
    addEventListener(type: any, listener: any, options?: any): void {
        this.ws.addEventListener(type,listener,options)
    }
    removeEventListener<K extends keyof WebSocketEventMap>(type: K, listener: (this: WebSocket, ev: WebSocketEventMap[K]) => any, options?: boolean | EventListenerOptions): void;
    removeEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | EventListenerOptions): void;
    removeEventListener(type: any, listener: any, options?: any): void {
        this.ws.removeEventListener(type,listener,options)
    }
    dispatchEvent(event: Event): boolean {
        return this.ws.dispatchEvent(event)
    }
}
/**
 * 给c3SDK专用连接
 */
export class JSJaCSDKWebSocketConnection extends JSJaCWebSocketConnection {

    private  ws:SDKWebSocket;
   constructor(oArg:ConnectionParam,ws:WebSocket) {
       super(oArg);
       ws=new SDKWebSocket(ws,oArg.oDbg)
       this.setWs(ws);
   }

   connect(oArg:LoginParam):void {
       super.connect(oArg)
       console.log('call sdkWebsocket onOpen')
       this._onopen()
       
   }
}